package com.atguigu.kafka.producer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class CustomProducer {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 0 配置
        Properties properties = new Properties();

        // 连接集群 bootstrap.servers
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.25.23:9092,192.168.25.23:9093,192.168.25.23:9094");

        // 指定对应的key和value的序列化类型 key.serializer
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        // 1 创建kafka生产者对象
        // "" hello
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        int count=0;
        // 2 发送数据
        while(true){
            System.out.println("第"+count+"次发送");
            Future<RecordMetadata> future = kafkaProducer.send(new ProducerRecord<>("my-topic", "atguigu" + count++));
            RecordMetadata recordMetadata = future.get();
            System.out.println(recordMetadata);
            Thread.sleep(100);
            if(count==1000000)break;
        }
//        for (int i = 0; i < 5; i++) {
//
//        }

        // 3 关闭资源
        kafkaProducer.close();
    }
}
